package com.hjy.cabinet.db;

import android.util.Log;

import com.hjy.cabinet.db.domain.OperationLog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OperationLogInstance {
    private static OperationLogInstance instance;

    private OperationLogDBHelper dbHelper;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private OperationLogInstance() {
    }

    public static OperationLogInstance getInstance() {
        if (instance == null) {
            synchronized (OperationLogInstance.class) {
                if (instance == null) {
                    instance = new OperationLogInstance();
                }
            }
        }
        return instance;
    }

    /**
     * 添加配置项
     *
     * @param operationLog
     * @return 返回插入的行数
     */
    public long addOperationLog(OperationLog operationLog) {
        try {
            return dbHelper.addOperationLog(operationLog.getUserId(), operationLog.getUsername(),
                    operationLog.getEvent(), operationLog.getOperationType(),
                    operationLog.getOperationTime(), operationLog.getOperationStatus());
        } catch (Exception e) {
            Log.e("OperationLogInstance", "Error adding operation log: " + e.getMessage(), e);
            return -1; // 返回-1表示失败
        }
    }

    /**
     * 更新配置项
     *
     * @param operationLog
     * @return 返回更新的行数
     */
    public int updateOperationLog(OperationLog operationLog) {
        try {
            return dbHelper.updateOperationLog(operationLog.getId(), operationLog.getUserId(),
                    operationLog.getUsername(), operationLog.getEvent(),
                    operationLog.getOperationType(), operationLog.getOperationTime(),
                    operationLog.getOperationStatus());
        } catch (Exception e) {
            Log.e("OperationLogInstance", "Error updating operation log: " + e.getMessage(), e);
            return 0; // 返回0表示没有行被更新
        }
    }

    /**
     * 删除配置项
     *
     * @param id 配置项ID
     * @return 返回删除的行数
     */
    public int deleteOperationLog(int id) {
        try {
            return dbHelper.deleteOperationLog(id);
        } catch (Exception e) {
            Log.e("OperationLogInstance", "Error deleting operation log: " + e.getMessage(), e);
            return 0; // 返回0表示没有行被删除
        }
    }

    /**
     * 获取配置项
     *
     * @param id 配置项ID
     * @return 返回配置项
     */
    public OperationLog getOperationLogById(int id) {
        try {
            return dbHelper.getOperationLogById(id);
        } catch (Exception e) {
            Log.e("OperationLogInstance", "Error retrieving operation log by ID: " + e.getMessage(), e);
            return null; // 返回null表示未找到
        }
    }

    /**
     * 获取所有配置
     *
     * @return List<OperationLog>
     */
    public List<OperationLog> getAllOperationLogs() {
        try {
            return dbHelper.getAllOperationLogs();
        } catch (Exception e) {
            Log.e("OperationLogInstance", "Error retrieving all operation logs: " + e.getMessage(), e);
            return new ArrayList<>(); // 返回空的列表
        }
    }

    /**
     * 获取所有没有同步的数据
     *
     * @return List<OperationLog>
     */
    public List<OperationLog> getOperationLogsByEvent() {
        try {
            return dbHelper.getOperationLogsByEvent();
        } catch (Exception e) {
            Log.e("OperationLogInstance", "Error retrieving operation logs by event: " + e.getMessage(), e);
            return new ArrayList<>(); // 返回空的列表
        }
    }

    // 根据用户名和时间段查询操作日志，支持分页和时间倒序
    public List<OperationLog> getOperationLogs(String username, String startDate, String endDate, int offset, int limit) {
        try {
            return dbHelper.getOperationLogs(username, startDate, endDate, offset, limit);
        } catch (Exception e) {
            Log.e("OperationLogInstance", "Error retrieving operation logs: " + e.getMessage(), e);
            return new ArrayList<>(); // 返回空的列表
        }
    }

    public void setInstance(OperationLogDBHelper operationLogDBHelper) {
        this.dbHelper = operationLogDBHelper;
    }

    public String getCurrentTime(){
        String currentTime = format.format(new Date());
        return currentTime;
    }
}